package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.UserLoginService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.constants.AdminConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @Author 96558
 * @Date 2021/1/10 9:53
 * @Version 1.0
 **/
@Service
public class UserLoginServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements UserLoginService {
    @Override
    public ResponseResult login(AdUserDto dto) {

        //1.校验参数
        if (dto==null || StringUtils.isBlank(dto.getName())
                        ||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行查询
        //根据用户精确查询有效用户
        AdUser adUser=getOne(Wrappers.<AdUser>lambdaQuery()
        .eq(AdUser::getName,dto.getName())
        .eq(AdUser::getStatus, AdminConstants.AD_USER_ISSTATUS)
        );
        if (adUser==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //比对密码
        //数据库密码
        String dbPwd = adUser.getPassword();
        String password = dto.getPassword();

        String salt = adUser.getSalt();
        //加密规则：password+salt
       String saltpwd= password+salt;
        String digestPwd = DigestUtils.md5DigestAsHex(saltpwd.getBytes());
        //返回结果
        if (!dbPwd.equals(digestPwd)){
            CustException.cust(AppHttpCodeEnum.ADMIN_USER_NAMEORPWD_ERROR);
        }
        //3.更新数据库登录时间
        adUser.setLoginTime(new Date());
        updateById(adUser);
        //4.生成token
        Map<String,Object>  resultMap=new HashMap<>();
        String token = AppJwtUtil.getToken(adUser.getId().longValue());
        resultMap.put("token",token);

        //用户信息
        adUser.setPassword(null);
        adUser.setSalt(null);
        resultMap.put("user",adUser);
        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult register(AdUser aduser) {
        //1.校验参数
        if (StringUtils.isEmpty(aduser.getName())||StringUtils.isEmpty(aduser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"用户名密码不能为空");
        }
        //2.业务处理
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        String password = aduser.getPassword();

        aduser.setPassword( DigestUtils.md5DigestAsHex((password + salt).getBytes()) );
        aduser.setSalt(salt);
        aduser.setCreatedTime(new Date());
        aduser.setStatus(0);
        save(aduser);
        //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
